#include <iostream>
#include <cstdio>
#include <pthread.h>
#include <unistd.h>

using namespace std;

pthread_t main_thread;

//void*是系统层面设计的一个通用接口
void *ThreadRoutine(void *args)
{
    //暂时的方案
    int i = *(int*)args;
    delete (int*)args;


    int cnt = 0;
    //while( cnt < 5 ){
    while( true ){
        cout << "thread index : " << i << " count : " << cnt << "thread id: " << pthread_self() <<  endl;
        sleep(1);
        cnt++;
	if(cnt == 3)
	{
		cout << "thread : " << i << " quit " << endl;
		break;
	}
    }
    //exit(10); //用来终止进程的！！！，绝对不能用来终止线程
    //return nullptr;
    pthread_exit((void*)5);
    //return nullptr;
}

int main()
{
    main_thread = pthread_self();
#define NUM 2
    pthread_t tids[NUM];
    for(auto i = 0; i < NUM; i++){
        int *p = new int(i);
        pthread_create(tids+i, nullptr, ThreadRoutine, p);
        cout << "create thread : " << tids[i] << "success" << endl; 
    }
    //cout << "#################################";
    //cout << tid0 << endl;
    //cout << tid1 << endl;
    //cout << tid2 << endl;
    //cout << tid3 << endl;
    //cout << "#################################";

    //sleep(5);

  //  for(auto i = 0; i < NUM; i++){
  //      pthread_cancel(tids[i]);
  //      cout << "thread : " << tids[i] << " been cancel!" << endl;
  //      sleep(1);
  //  }


    while(true){
        cout << "main thread is running...: main thread id : " << pthread_self() << endl;
        sleep(1);
    }

    return 0;
}

